//
//  YWNoQrcodeSelectLocationTableViewController.m
//  maintain
//
//  Created by kyo on 16/12/7.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNoQrcodeSelectLocationTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "YWConst.h"
#import "YWCommonTool.h"
#import "YWLoginTool.h"

#import "YWBuilding.h"
#import "YWBuildingManager.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWLocation.h"
#import "YWDictMaintenanceType.h"

#import "YWSelectBuildingTableViewController.h"
#import "YWSelectApartmentTableViewController.h"
#import "YWSelectFloorTableViewController.h"
#import "YWSelectLocationTableViewController.h"

#import "YWPatrollineRecord.h"
#import "YWDoKeepWatchMissionTableViewController.h"
#import "YWInspectionMissionModel.h"
//#import "YWDoTempEquipmentInspectionTableViewController.h"
#import "YWDoTempEquipmentMissionTableViewController.h"
#import "YWMission.h"
//#import "YWDoTempDeteAndUpKeepMissionTableViewController.h"
#import "YWDoTempDeteAndUpKeepEquTypeMissionTableViewController.h"
#import "YWDoTempTroubleMissionTableViewController.h"
#import "YWSelectMeterReadingEquipmentTableViewController.h"
#import "YWEquipmentManager.h"
#import "YWTroubleMissionModel.h"
#import "YWReportTroubleModel.h"
#import "YWTroubleMissionManager.h"
#import "YWReportTroubleManager.h"

//static NSString *const KYWRememberLastLocation = @"KYWRememberLastLocation.data";

@interface YWNoQrcodeSelectLocationTableViewController ()<YWSelectBuildingTableViewControllerDelegate,YWSelectApartmentTableViewControllerDelegate,YWSelectFloorTableViewControllerDelegate,YWSelectLocationTableViewControllerDelegate>{
    YWBuilding *currentBuilding;
    YWApartment *currentApartment;
    YWFloor *currentFloor;
    YWLocation *currentLocation;
}
@property (nonatomic, strong) NSArray *allBuildings;
@end

@implementation YWNoQrcodeSelectLocationTableViewController

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"selectLocation", nil);//@"选择位置";
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"sure", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBarButton:)];
    
    //加载上次记忆的楼栋楼层等信息
//    if (!_isFromSubscrip) {
        [self loadLastRememberInfo];
//    }
//    if (_isFromSubscrip) {
//       self.navigationItem.rightBarButtonItem.enabled = YES;
//    }else{
        if (currentLocation) {
            self.navigationItem.rightBarButtonItem.enabled = YES;
        }else{
            self.navigationItem.rightBarButtonItem.enabled = NO;
        }
//    }
    
    [self initTableView];
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.sectionFooterHeight = 1.0;
    self.tableView.rowHeight = 44.0f;
}

- (NSArray *)allBuildings{
    if (!_allBuildings) {
        if (_lineId > 0) {
            _allBuildings = [[YWBuildingManager shareManager] buildingArrayWithLineId:_lineId type:_isKeepWatch lineType:_isEquipmentLine];
        }else if (_missionId >0){
            _allBuildings = [[YWBuildingManager shareManager] selectPositionWithMissionId:_missionId equipmentTypeId:_equipmentTypeId positionType:1 buildingId:nil apartmentId:0 floorId:0];
        }else{
            _allBuildings = [[YWBuildingManager shareManager] allBuilding];
        }
    }
    return _allBuildings;
}

- (NSArray *)allApartments{
    NSArray *allApartments = nil;
    if (_lineId>0) {
        allApartments = [[NSArray alloc] initWithArray:[[YWBuildingManager shareManager] apartmentArrayWithBuildingId:currentBuilding.buildingId LineId:_lineId type:_isKeepWatch lineType:_isEquipmentLine]];
    }else if (_missionId >0){
        allApartments = [[YWBuildingManager shareManager] selectPositionWithMissionId:_missionId equipmentTypeId:_equipmentTypeId positionType:2 buildingId:currentBuilding.buildingId apartmentId:0 floorId:0];
    }else{
        allApartments = [[NSArray alloc] initWithArray:[[YWBuildingManager shareManager] apartmentWithbuildingId:currentBuilding.buildingId]];
    }
    return allApartments;
}

- (NSArray *)allFloors{
    NSArray *allFloors = nil;
    if (_lineId >0) {
        allFloors = [[NSArray alloc] initWithArray:[[YWBuildingManager shareManager] floorArrayWithApartmentId:currentApartment.apartmentId LineId:_lineId type:_isKeepWatch lineType:_isEquipmentLine]];
    }else if (_missionId >0){
        allFloors = [[YWBuildingManager shareManager] selectPositionWithMissionId:_missionId equipmentTypeId:_equipmentTypeId positionType:3 buildingId:nil apartmentId:currentApartment.apartmentId floorId:0];
    }else{
        allFloors = [[NSArray alloc] initWithArray:[[YWBuildingManager shareManager] floorWithApartmentId:currentApartment.apartmentId]];
    }
    return allFloors;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseIdentifier = @"YWNoQrcodeSelectLocatoinCellReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        cell.textLabel.font = kContentFont;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    [self configureCell:cell withIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell*)cell withIndexPath:(NSIndexPath*)indexPath{
    if (indexPath.section == 0) {
        NSString *buildingShowName = [YWLoginTool buildingShowName];
        buildingShowName = buildingShowName.length ? buildingShowName:NSLocalizedString(@"building", nil);
        cell.textLabel.text = currentBuilding.buildingName.length ? currentBuilding.buildingName : [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),buildingShowName];
    }else if (indexPath.section ==1){
        NSString *apartmentShowName = [YWLoginTool apartmentShowName];
        apartmentShowName = apartmentShowName.length ? apartmentShowName:NSLocalizedString(@"apartment", nil);
        cell.textLabel.text = currentApartment.apartmentName.length ? currentApartment.apartmentName : [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),apartmentShowName];
    }else if (indexPath.section ==2){
        NSString *floorShowName = [YWLoginTool floorShowName];
        floorShowName = floorShowName.length ? floorShowName:NSLocalizedString(@"floor", nil);
        cell.textLabel.text = currentFloor.floorName.length ? currentFloor.floorName : [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),floorShowName];
    }else if (indexPath.section  ==3){
        NSString *locationShowName = [YWLoginTool locationShowName];
        locationShowName = locationShowName.length ? locationShowName:NSLocalizedString(@"position", nil);
        cell.textLabel.text = currentLocation.locationName.length ? currentLocation.locationName : [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),locationShowName];
    }
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString* HeaderIdentifier = @"YWNoQrcodeSelectLocatoinTableViewControllerForHeaderInSection";
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor customTextGrayColor];
        label.tag = 2006;
        label.numberOfLines = 0;
        label.font = KSmallContentFont;
        [headerView addSubview:label];
    }
    NSString *headText = [self titleForHeaderInSection:section];
    UILabel* label = [headerView viewWithTag:2006];
    label.text = headText;
    label.frame = CGRectMake(15, 0, SCREEN_WIDTH - 2 * 15, 35);
    return headerView;
}

-(NSString *)titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        NSString *buildingShowName = [YWLoginTool buildingShowName];
        return buildingShowName.length?[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),buildingShowName]:NSLocalizedString(@"selectBuilding", nil);
    }else if (section == 1) {
        NSString *apartmentShowName = [YWLoginTool apartmentShowName];
        return apartmentShowName.length?[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),apartmentShowName]:NSLocalizedString(@"selectApartment", nil);
    }else if (section == 2){
        NSString *floorShowName = [YWLoginTool floorShowName];
        return floorShowName.length?[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),floorShowName]:NSLocalizedString(@"selectFloor", nil);
    }else if (section == 3){
        NSString *locationShowName = [YWLoginTool locationShowName];
        return locationShowName.length?[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),locationShowName]:NSLocalizedString(@"selectLocation", nil);
    }else{
        return NSLocalizedString(@"none", nil);
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        YWSelectBuildingTableViewController *selectBuildingVC = [[YWSelectBuildingTableViewController alloc] init];
        selectBuildingVC.delegate = self;
        selectBuildingVC.ownBuilding = currentBuilding;
        [self.navigationController pushViewController:selectBuildingVC animated:YES];
    }else if (indexPath.section == 1){
        if (currentBuilding.buildingName.length) {
            YWSelectApartmentTableViewController *selectApartmentVC = [[YWSelectApartmentTableViewController alloc] init];
            selectApartmentVC.delegate = self;
            selectApartmentVC.canEdit = NO;
            selectApartmentVC.building = currentBuilding;
            
            selectApartmentVC.lineId = _lineId;
            selectApartmentVC.isKeepWatch = _isKeepWatch;
            selectApartmentVC.isEquipmentLine = _isEquipmentLine;
            
            selectApartmentVC.missionId = _missionId;
            selectApartmentVC.equipmentTypeId = _equipmentTypeId;
            
            selectApartmentVC.ownApartment = currentApartment;
            [self.navigationController pushViewController:selectApartmentVC animated:YES];
        }else{
            NSString *buildingShowName = [YWLoginTool buildingShowName];
            [YWCommonTool showTextHUDWithContent:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"firstSelect", nil),buildingShowName?:NSLocalizedString(@"building", nil)]];
        }
    }else if (indexPath.section == 2){
        if (currentApartment.apartmentName.length) {
            YWSelectFloorTableViewController *selectFloorVC = [[YWSelectFloorTableViewController alloc] init];
            selectFloorVC.delegate = self;
            selectFloorVC.canEdit = NO;
            selectFloorVC.apartment = currentApartment;
            
            selectFloorVC.lineId = _lineId;
            selectFloorVC.isKeepWatch = _isKeepWatch;
            selectFloorVC.isEquipmentLine = _isEquipmentLine;
            
            selectFloorVC.missionId = _missionId;
            selectFloorVC.equipmentTypeId = _equipmentTypeId;
            
            selectFloorVC.ownFloor = currentFloor;
            [self.navigationController pushViewController:selectFloorVC animated:YES];
        }else{
            NSString *apartmentShowName = [YWLoginTool apartmentShowName];
            [YWCommonTool showTextHUDWithContent:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"firstSelect", nil),apartmentShowName?:NSLocalizedString(@"apartment", nil)]];
        }
    }else if (indexPath.section == 3){
        if (currentFloor.floorName.length) {
            YWSelectLocationTableViewController *selectLocationVC = [[YWSelectLocationTableViewController alloc] init];
            selectLocationVC.delegate = self;
            selectLocationVC.canEdit = NO;
            selectLocationVC.floor = currentFloor;
            
            selectLocationVC.lineId = _lineId;
            selectLocationVC.isKeepWatch = _isKeepWatch;
            selectLocationVC.isEquipmentLine = _isEquipmentLine;
            
            selectLocationVC.missionId = _missionId;
            selectLocationVC.equipmentTypeId = _equipmentTypeId;
            
            selectLocationVC.ownLocation = currentLocation;
            [self.navigationController pushViewController:selectLocationVC animated:YES];
        }else{
            NSString *floorShowName = [YWLoginTool floorShowName];
            [YWCommonTool showTextHUDWithContent:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"firstSelect", nil),floorShowName?:NSLocalizedString(@"floor", nil)]];
        }
    }
}
#pragma mark - YWSelectBuildingTableViewControllerDelegate
-(void)popSelectedBuilding:(YWBuilding *)building{
    if (building && ![currentBuilding.buildingId isEqualToString:building.buildingId]) {
        currentBuilding = building;
        currentApartment = nil;
        currentFloor = nil;
        currentLocation = nil;
        [self.tableView reloadData];
    }
}
#pragma mark - YWSelectApartmentTableViewControllerDelegate
- (void)popSelectedApartment:(YWApartment *)apartment{
    if (apartment && currentApartment.apartmentId != apartment.apartmentId) {
        currentApartment = apartment;
        currentFloor = nil;
        currentLocation = nil;
        [self.tableView reloadData];
    }
}
#pragma mark - YWSelectFloorTableViewControllerDelegate
- (void)popSelectedFloor:(YWFloor *)floor{
    if (floor && currentFloor.floorId != floor.floorId){
        currentFloor = floor;
        currentLocation = nil;
        [self.tableView reloadData];
    }
}
#pragma mark - YWSelectLocationTableViewControllerDelegate
- (void)popSelectedLocation:(YWLocation *)location{
    if (location && currentLocation.locationId != location.locationId) {
        currentLocation = location;
        self.navigationItem.rightBarButtonItem.enabled = YES;
        [self.tableView reloadData];
    }
}

- (NSString*)savefilePath:(NSString*)fileName
{
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:fileName];
    return filePath;
}

- (void)loadLastRememberInfo{
    YWBuilding *building = [NSKeyedUnarchiver unarchiveObjectWithFile:[self savefilePath:KYWRememberLastBuilding]];
    if (building || _buildingId.length) {
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K == %@", @"buildingId", _buildingId.length ? _buildingId : building.buildingId];
        NSArray *array = [self.allBuildings filteredArrayUsingPredicate:searchPredicate];
        if (array.count > 0) {
            currentBuilding = building;
            YWApartment *apartment = [NSKeyedUnarchiver unarchiveObjectWithFile:[self savefilePath:KYWRememberLastApartment]];
            if (apartment) {
                NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"apartmentId", apartment.apartmentId];
                NSArray *array = [[self allApartments] filteredArrayUsingPredicate:searchPredicate];
                if (array.count > 0) {
                    currentApartment = apartment;
                    YWFloor *floor = [NSKeyedUnarchiver unarchiveObjectWithFile:[self savefilePath:KYWRememberLastFloor]];
                    if (floor) {
                        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"floorId", floor.floorId];
                        NSArray *array = [[self allFloors] filteredArrayUsingPredicate:searchPredicate];
                        if (array.count >0) {
                            currentFloor = floor;
                        }
                    }
                }
                
            }
            
        }
    }
    if (currentBuilding == nil) {
        currentBuilding = self.allBuildings.count>0?self.allBuildings[0]:nil;
    }
}

- (void)clickRightBarButton:(id)sender{
    if (currentBuilding && currentApartment && currentFloor && currentLocation) {
        //记忆楼盘
        [NSKeyedArchiver archiveRootObject:currentBuilding toFile:[self savefilePath:KYWRememberLastBuilding]];
        //记忆楼栋
        [NSKeyedArchiver archiveRootObject:currentApartment toFile:[self savefilePath:KYWRememberLastApartment]];
        //记忆楼层
        [NSKeyedArchiver archiveRootObject:currentFloor toFile:[self savefilePath:KYWRememberLastFloor]];
    }
    if (_isDoTempMission) {
        if (currentLocation.locationId>0) {
            if (_maintenanceType == YWMaintenanceTypeKeepWatch) {
                [self doTempKeepWatchMission];
            }else{
                BOOL isExistEqu = [[YWEquipmentManager shareManager] isExistEquipmentWithLocationId:currentLocation.locationId];
                if (isExistEqu) {
                    //做临时的任务
                    if (_maintenanceType == YWMaintenanceTypePatrol) {
                        [self doTempEquipmentInspection];
                    }else if (_maintenanceType == YWMaintenanceTypeDetection || _maintenanceType == YWMaintenanceTypeUpkeep){
                        [self doTempDetectAndKeepUpMission];
                    }else if (_maintenanceType == YWMaintenanceTypeTrouble){
                        [self doTempTroubleMission];
                    }
                }else{
                    [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"noEquipmentInTheLocation", nil)];
                }
            }
        }else{
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"pleaseSelectOneLocation", nil)];
        }
    }
//    else if (_isFromSubscrip){
//        
//        NSMutableArray * tempArray = [NSMutableArray array];
//        NSMutableArray * bigArray = [NSMutableArray array];
//        [self.selLocArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            if ([obj isKindOfClass:[YWBuilding class]]) {
//                YWBuilding *building = (YWBuilding*)obj;
//                if (currentLocation) {
//                    if ([currentBuilding.buildingId isEqualToString:building.buildingId]) {
//                        [tempArray addObject:obj];
//                    }
//                }else if (currentFloor && !currentLocation){
//                    if ([currentBuilding.buildingId isEqualToString:building.buildingId]) {
//                        [tempArray addObject:obj];
//                    }
//                }else if (currentApartment && !currentFloor){
//                    if ([currentBuilding.buildingId isEqualToString:building.buildingId]) {
//                        [tempArray addObject:obj];
//                    }
//                }else{
//                    if ([currentBuilding.buildingId isEqualToString:building.buildingId]) {
//                        [bigArray addObject:obj];
//                    }
//                }
//            }else if ([obj isKindOfClass:[YWApartment class]]){
//                YWApartment *apartment = (YWApartment*)obj;
//                if (currentLocation) {
//                    if (currentApartment.apartmentId == apartment.apartmentId) {
//                        [tempArray addObject:obj];
//                    }
//                }else if (currentFloor && !currentLocation){
//                    if (currentApartment.apartmentId == apartment.apartmentId) {
//                        [tempArray addObject:obj];
//                    }
//                }else if (currentApartment && !currentFloor){
//                    if (currentApartment.apartmentId == apartment.apartmentId) {
//                        [bigArray addObject:obj];
//                    }
//                }else if (currentBuilding && !currentApartment){
//                    if ([apartment.buildingId isEqualToString:currentBuilding.buildingId]) {
//                        [bigArray addObject:obj];
//                    }
//                }
//            }else if ([obj isKindOfClass:[YWFloor class]]){
//                YWFloor *floor = (YWFloor*)obj;
//                YWApartment *apartment = [[YWBuildingManager shareManager]apartmentWithId:floor.apartmentId];
//                if (currentLocation) {
//                    if (currentFloor.floorId == floor.floorId) {
//                        [tempArray addObject:obj];
//                    }
//                }else if (currentFloor && !currentLocation){
//                    if (currentFloor.floorId == floor.floorId) {
//                        [bigArray addObject:obj];
//                    }
//                }else if (currentApartment && !currentFloor){
//                    if (apartment.apartmentId == currentApartment.apartmentId) {
//                        [bigArray addObject:obj];
//                    }
//                }else if (currentBuilding && !currentApartment){
//                    if ([apartment.buildingId isEqualToString:currentBuilding.buildingId]) {
//                        [bigArray addObject:obj];
//                    }
//                }
//            }else if ([obj isKindOfClass:[YWLocation class]]){
//                YWLocation *location = (YWLocation*)obj;
//                YWFloor *floor = [[YWBuildingManager shareManager] floorWithId:location.floorId];
//                YWApartment *apartment = [[YWBuildingManager shareManager]apartmentWithId:floor.apartmentId];
//                if (currentLocation) {
//                    if (currentLocation.locationId == location.locationId) {
//                        [bigArray addObject:obj];
//                    }
//                }else if (currentFloor && !currentLocation){
//                    if (currentFloor.floorId == floor.floorId) {
//                        [bigArray addObject:obj];
//                    }
//                }else if (currentApartment && !currentFloor){
//                    if (currentApartment.apartmentId == apartment.apartmentId) {
//                        [bigArray addObject:obj];
//                    }
//                }else if (currentBuilding){
//                    if ([currentBuilding.buildingId isEqualToString:apartment.buildingId]) {
//                        [bigArray addObject:obj];
//                    }
//                }
//            }
//        }];
//
//        if (tempArray.count>0) {
//            NSString *tip = @"您之前选的位置已包含现在的位置，确定选择更小的位置吗？";
//            UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"提示" message:tip preferredStyle:UIAlertControllerStyleAlert];
//            [self presentViewController:alertController animated:YES completion:nil];
//            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
//            [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                [self.selLocArray removeObjectsInArray:tempArray];
//                if (currentLocation) {
//                    [self.selLocArray addObject:currentLocation];
//                }else if (currentFloor && !currentLocation){
//                    [self.selLocArray addObject:currentFloor];
//                }else if (currentApartment && !currentFloor){
//                    [self.selLocArray addObject:currentApartment];
//                }else if (currentBuilding && !currentApartment){
//                    [self.selLocArray addObject:currentBuilding];
//                }
//                if ([self.delegate respondsToSelector:@selector(selectLocationArray:)]) {
//                    [self.delegate selectLocationArray:self.selLocArray];
//                }
//                [self.navigationController popViewControllerAnimated:YES];
//            }]];
//        }else{
//            [self.selLocArray removeObjectsInArray:bigArray];
//            if (currentLocation) {
//                [self.selLocArray addObject:currentLocation];
//            }else if (currentFloor && !currentLocation){
//                [self.selLocArray addObject:currentFloor];
//            }else if (currentApartment && !currentFloor){
//                [self.selLocArray addObject:currentApartment];
//            }else if (currentBuilding && !currentApartment){
//                [self.selLocArray addObject:currentBuilding];
//            }
//            if (currentBuilding) {
//                if ([self.delegate respondsToSelector:@selector(selectLocationArray:)]) {
//                    [self.delegate selectLocationArray:self.selLocArray];
//                }
//                [self.navigationController popViewControllerAnimated:YES];
//            }else{
//                [YWCommonTool showTextHUDWithContent:@"请选择位置等信息"];
//            }
//        }
//        
//    }
    else{
        if (_doMissionBlock) {
            if (currentLocation.locationId>0) {
                if (_buildingId.length && ![currentBuilding.buildingId isEqualToString:_buildingId]) {
                    [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"buildingNotMatchTheMission", nil)];
                }else{
                    self.doMissionBlock(currentLocation.locationId);
                }
            }else{
                [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"pleaseSelectOneLocation", nil)];
            }
        }else{
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"failedAndTryAgain", nil)];
        }
    }
}

- (void)doTempEquipmentInspection{
    //临时设备巡查
    if (currentLocation.locationId>0) {
        YWDoTempEquipmentMissionTableViewController *doTempEquipmentInspectionVC = [[YWDoTempEquipmentMissionTableViewController alloc] init];
        doTempEquipmentInspectionVC.locationId = currentLocation.locationId;
        doTempEquipmentInspectionVC.qrcodeID = nil;
        doTempEquipmentInspectionVC.currentEquipmentId = nil;
        doTempEquipmentInspectionVC.inspectionMission = [[YWInspectionMissionModel alloc] init];
        doTempEquipmentInspectionVC.isFromLedger = YES;
        [self.navigationController pushViewController:doTempEquipmentInspectionVC animated:YES];
    }else{
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"pleaseSelectOneLocation", nil)];
    }
}

- (void)doTempKeepWatchMission{
    //临时巡更
    if (currentLocation.locationId) {
        YWPatrollineRecord *record = [[YWPatrollineRecord alloc]init];
        NSString *uniqueTag = [NSString stringWithFormat:@"temp_%lld",(long long)[YWCommonTool negativeTimestampId]/1000];
        record.uniqueTag = uniqueTag;
        record.count = 0;
        NSTimeInterval nowTime = [[NSDate date] timeIntervalSince1970]*1000;
        record.startTime = nowTime;
        record.endTime = nowTime;
        record.result = YWPatrollineRecordResultUnfinishAndNormal;
        YWLoginUser *currentUser = [YWLoginTool loginUser];
        record.executorIds = @(currentUser.uniqueId).stringValue;
        //            record.keepWatchMissionId = 0;
        //            页面跳转
        YWDoKeepWatchMissionTableViewController *writeKeepWatchInfoVC = [YWDoKeepWatchMissionTableViewController new];
        writeKeepWatchInfoVC.tempPatrolLineRecord = record;
        NSInteger locationId = currentLocation.locationId;
        writeKeepWatchInfoVC.locationId = locationId;
        writeKeepWatchInfoVC.qrcodeId = nil;
        writeKeepWatchInfoVC.isFromLedger = YES;
        [self.navigationController pushViewController:writeKeepWatchInfoVC animated:YES];
    }else{
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"pleaseSelectOneLocation", nil)];
    }
}

- (void)doTempDetectAndKeepUpMission{
    //临时检测,保养
    if (currentLocation.locationId>0) {
        YWDoTempDeteAndUpKeepEquTypeMissionTableViewController *missionVC = [[YWDoTempDeteAndUpKeepEquTypeMissionTableViewController alloc]init];
        missionVC.locationId = currentLocation.locationId;
        missionVC.qrCodeId = nil;
//        missionVC.currentEquipmentId = nil;
        missionVC.maintenanceType = _maintenanceType;
        [self.navigationController pushViewController:missionVC animated:YES];
    }else{
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"pleaseSelectOneLocation", nil)];
    }
}

- (void)doTempTroubleMission{
    //临时维修
    if (currentLocation.locationId>0) {
        YWDoTempTroubleMissionTableViewController *troubleVC = [[YWDoTempTroubleMissionTableViewController alloc]init];
        troubleVC.troubleMissionModel = [self createTempTroubelMissionModel];
        [self.navigationController pushViewController:troubleVC animated:YES];
    }else{
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"pleaseSelectOneLocation", nil)];
    }
}


- (YWTroubleMissionModel*)createTempTroubelMissionModel{
    YWTroubleMissionModel * troubleMissionModel = [[YWTroubleMissionModel alloc] init];
    YWReportTroubleModel * reportTrouble = [[YWReportTroubleModel alloc] init];
    reportTrouble.troubleId = -1 * [YWCommonTool randNumber];
    reportTrouble.qrcodeId = nil;
    reportTrouble.location = currentLocation;
    reportTrouble.equipment = nil;
    reportTrouble.maintenanceTypeId = YWMaintenanceTypeTrouble;
    reportTrouble.troubleStatus = YWTroubleMissionStatusFinish;
    
    //    NSInteger locationId = [[YWEquipmentManager shareManager]locationIdWithQrcodeId:self.qrcodeId];
    //    YWBuilding* building = [[YWBuildingManager shareManager] buildingWithLocationId:locationId];
    reportTrouble.building = currentBuilding;
    troubleMissionModel.trouble = reportTrouble;
    troubleMissionModel.troubleStatus = YWTroubleMissionStatusFinish;
    return troubleMissionModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
